/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "frame.h"

static const char class_name [] = "EGUI Widget Frame";

static int afc_easygui_internal_frame_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_frame_start ( DynamicClass * dyn );
static int afc_easygui_internal_cb_frame_end ( DynamicClass * dyn );

static int afc_easygui_frame_method_add ( DynamicClass * dyn );


DynamicClass * afc_easygui_internal_frame_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynamic class: frame" );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_frame_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_frame_start );
	afc_dynamic_class_add_method ( dyn, "close_callback", NULL,  afc_easygui_internal_cb_frame_end );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "add", NULL,  		afc_easygui_frame_method_add );

	return ( dyn );
}

int afc_easygui_internal_frame_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	/*
	EasyGUIWidgetData * ewd;

	if ( dyn == NULL ) return ( AFC_ERR_INVALID_POINTER );
	
	WIDGET_FREE_EWIDGET ( dyn, ewd );

	afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
	*/
}

static int afc_easygui_internal_frame_get_template ( DynamicClass * dyn )
{
	// Valid values of Shadow:
	//
	//	IN
	//	OUT
	//	EIN
	//	EOUT

	dyn->result = "TITLE SHADOW";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_frame_start ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	GtkWidget * w;
	char * title;
	char * shadow;
	int shad = GTK_SHADOW_ETCHED_IN;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	afc_easygui_widget_parse_default_params ( egui );

	title   = afc_cmd_parser_arg_get_by_name ( egui->parser, "TITLE" ); 
	shadow  = afc_cmd_parser_arg_get_by_name ( egui->parser, "SHADOW" ); 

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "frame" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );

	w = gtk_frame_new ( title );

	if ( shadow )
	{
		if ( strcasecmp ( shadow, "IN" ) == 0 )
			shad = GTK_SHADOW_IN;
		else if ( strcasecmp ( shadow, "OUT" ) == 0 )
			shad = GTK_SHADOW_OUT;
		else if ( strcasecmp ( shadow, "EIN" ) == 0 )
			shad = GTK_SHADOW_ETCHED_IN;
		else if ( strcasecmp ( shadow, "EOUT" ) == 0 )
			shad = GTK_SHADOW_ETCHED_OUT;
		else if ( strcasecmp ( shadow, "NONE" ) == 0 )
			shad = GTK_SHADOW_NONE;
	}

	gtk_frame_set_shadow_type ( GTK_FRAME ( w ), shad );

	if ( egui->def_params->border ) gtk_container_set_border_width ( GTK_CONTAINER ( w ), egui->def_params->border );

	return ( afc_easygui_context_add_container ( context, w, dc, AFC_EASYGUI_CONTAINER_TYPE_FRAME, AFC_EASYGUI_CONTAINER_HOMOGENEOUS_NONE ) );
}

static int afc_easygui_internal_cb_frame_end ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;

	return ( afc_easygui_context_close_container ( egui->curr_context ) );
}

// METHODS
static int afc_easygui_frame_method_add ( DynamicClass * dyn )
{
	GtkWidget * w;
	EasyGUIWidgetData * ewidget;

	w = afc_array_master_first ( dyn->args );
	WIDGET_GET_EWIDGET ( dyn, ewidget );	

	gtk_container_add ( GTK_CONTAINER ( ewidget->gtk_handle ), w );

	return ( AFC_ERR_NO_ERROR );
}
